package com.fan.analysis.module.preprocessing.fusion.service;

import com.fan.analysis.common.domain.ResponseDTO;
import com.fan.analysis.module.preprocessing.fusion.dao.FusionDao;
import com.fan.analysis.module.preprocessing.fusion.domain.bo.CallCountBo;
import com.fan.analysis.module.preprocessing.fusion.domain.vo.FusionVo;
import com.fan.analysis.util.Keyword;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 融合记录服务
 *
 * @author fan
 * @date 2021/09/20 12:56
 **/
@Service
@Slf4j
public class FusionService {

    @Autowired
    private FusionDao fusionDao;
    @Autowired
    private AsyncService asyncService;

    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<FusionVo> dataFusion(String phoneNum) {

        // -------------- 1、通话记录条数统计 ------------------------
        FusionVo fusionVo = new FusionVo();
        // 1.1 统计主叫为该电话的数据
        List<CallCountBo> callCountBoCallingNum = fusionDao.callCountByCallingNum(phoneNum);
        // 1.2 统计被叫为该电话的数据
        List<CallCountBo> callCountBoCalledNum = fusionDao.callCountByCalledNum(phoneNum);
        fusionVo.setCall_num(callCountBoCallingNum.size() + callCountBoCalledNum.size());
        // -------------- 2、常用常用联系时间段的统计 ------------------------
        fusionVo.setContact_time_num(callCountBoCallingNum.size() + callCountBoCalledNum.size());
        // 异步更新
        asyncService.asyncCallFusion(phoneNum, callCountBoCallingNum, callCountBoCalledNum);

        // -------------- 3、短信记录条数的统计 ------------------------
        List<CallCountBo> countBySendNum = fusionDao.messageCountBySendNum(phoneNum);
        List<CallCountBo> countByRecNum = fusionDao.messageCountByRecNum(phoneNum);
        fusionVo.setMessage_num(countBySendNum.size() + countByRecNum.size());
        asyncService.asyncMessageFusion(phoneNum, countBySendNum, countByRecNum);
        // -------------- 4、关键词的统计 ------------------------

        // 查询发送方与接收方为该电话的短信通讯信息
        List<String> contentList = fusionDao.contentByPhoneNum(phoneNum);
        StringBuilder contents = new StringBuilder();
        for (String s : contentList) {
            contents.append(s);
        }

        try {
            List<String> keywords = Keyword.keywords(String.valueOf(contents));
            System.out.println(keywords);

            String s = keywords.toString().replaceAll(" ", "").replace("]", "").replace("[", "");
            // 更新用户的关键词
            fusionDao.updateUserKeywords(phoneNum, s);

        } catch (Exception e) {
            e.printStackTrace();
        }

        // 统计的结果先返回
        return ResponseDTO.succData(fusionVo);
    }


}
